﻿using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine.Networking;

public class SyncDict<TKey, TValue>
{
    public Action<TKey, TValue> Callback;
    public Dictionary<TKey, TValue> dict;
    private Dictionary<TKey, int> keyValueLookup;
    private SyncList<TKey> syncKeys;
    private SyncList<TValue> syncValues;

    public SyncDict(SyncList<TKey> syncKeys, SyncList<TValue> syncValues)
    {
        <SyncDict>c__AnonStorey71<TKey, TValue> storey = new <SyncDict>c__AnonStorey71<TKey, TValue> {
            syncKeys = syncKeys,
            syncValues = syncValues
        };
        this.dict = new Dictionary<TKey, TValue>();
        this.keyValueLookup = new Dictionary<TKey, int>();
        storey.<>f__this = (SyncDict<TKey, TValue>) this;
        this.syncKeys = storey.syncKeys;
        this.syncValues = storey.syncValues;
        if (storey.syncKeys.Count != storey.syncValues.Count)
        {
            throw new Exception("Cound not create SyncDict, key value list out of sync");
        }
        storey.syncValues.Callback = new SyncList<TValue>.SyncListChanged(storey.<>m__11E);
    }

    public TValue GetValue(TKey key)
    {
        TValue local;
        this.dict.TryGetValue(key, out local);
        return local;
    }

    public void OnStartClient()
    {
        if (this.syncKeys.Count != this.syncValues.Count)
        {
            throw new Exception("Cound not create SyncDict, key value list out of sync");
        }
        for (int i = 0; i < this.syncKeys.Count; i++)
        {
            TKey local = this.syncKeys[i];
            TValue local2 = this.syncValues[i];
            this.dict[local] = local2;
            this.keyValueLookup[local] = i;
        }
    }

    public void SetValue(TKey key, TValue value)
    {
        if (!this.dict.ContainsKey(key))
        {
            this.syncKeys.Add(key);
            this.syncValues.Add(value);
            this.dict[key] = value;
            this.keyValueLookup[key] = this.syncKeys.Count - 1;
        }
        TValue local = this.dict[key];
        if (!local.Equals(value))
        {
            this.syncValues[this.keyValueLookup[key]] = value;
            this.dict[key] = value;
        }
    }

    [CompilerGenerated]
    private sealed class <SyncDict>c__AnonStorey71
    {
        internal SyncDict<TKey, TValue> <>f__this;
        internal SyncList<TKey> syncKeys;
        internal SyncList<TValue> syncValues;

        internal void <>m__11E(SyncList<TValue>.Operation op, int i)
        {
            TKey local = this.syncKeys[i];
            TValue local2 = this.syncValues[i];
            this.<>f__this.dict[local] = local2;
            if (this.<>f__this.Callback != null)
            {
                this.<>f__this.Callback(local, local2);
            }
        }
    }
}

